﻿using System;
using System.Data;
using System.Globalization;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Data.Common;
using System.Text;
using System.Reflection;
using System.IO;
using Newtonsoft.Json;
using System.Globalization;

namespace OA.Utility
{
    /// <summary>
    /// JsonHelper 的摘要说明
    /// </summary>
    public class JsonHelper
    {
        /// <summary>   
        /// List转成json    
        /// </summary>   
        /// <typeparam name="T"></typeparam>   
        /// <param name="jsonName"></param>   
        /// <param name="list"></param>   
        /// <returns></returns>   
        public static string ListToJson<T>(IList<T> list, string jsonName)
        {
            StringBuilder json = new StringBuilder();
            if (string.IsNullOrEmpty(jsonName))
                jsonName = list[0].GetType().Name;
            json.Append("{\"" + jsonName + "\":[");
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    PropertyInfo[] pi = obj.GetType().GetProperties();
                    json.Append("{");
                    for (int j = 0; j < pi.Length; j++)
                    {
                        object propertyValue = pi[j].GetValue(list[i], null);
                        if (propertyValue != null)
                        {
                            Type type = propertyValue.GetType();
                            json.AppendFormat("\"{0}\":{1}", pi[j].Name,
                                              StringFormat(pi[j].GetValue(list[i], null).ToString(), type));
                        }
                        else
                        {
                            json.Append("\"" + pi[j].Name.ToString(CultureInfo.InvariantCulture) + "\":\"\"");
                        }
                        if (j < pi.Length - 1)
                        {
                            json.Append(",");
                        }
                    }
                    json.Append("}");
                    if (i < list.Count - 1)
                    {
                        json.Append(",");
                    }
                }
            }
            json.Append("]}");
            return json.ToString();
        }

        /// <summary>
        /// List转成json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="page">页码</param>
        /// <param name="rows">页码数据大小</param>
        /// <param name="totalcount">数据总数 </param>
        /// <returns></returns>
        public static string ListToGridJson<T>(IList<T> list, int page, int rows, int totalcount)
        {
            page = (page == 0) ? 1 : page;
            rows = (rows == 0) ? 10 : rows;
            int start = (page - 1) * rows;
            int end = page * rows;
            end = (end > totalcount) ? totalcount : end;
            StringBuilder json = new StringBuilder();
            json.Append("{\"total\":" + totalcount + ",");
            json.Append("\"rows\":[");
            if (list.Count > 0)
            {
                for (int i = start; i < end; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    PropertyInfo[] pi = obj.GetType().GetProperties();
                    json.Append("{");
                    for (int j = 0; j < pi.Length; j++)
                    {
                        object propertyValue = pi[j].GetValue(list[i], null);
                        if (propertyValue != null)
                        {
                            Type type = propertyValue.GetType();
                            string name = pi[j].Name;
                            json.Append("\"" + name.ToString(CultureInfo.InvariantCulture) + "\":" +
                                        StringFormat(pi[j].GetValue(list[i], null).ToString(), type));
                        }
                        else
                        {
                            json.Append("\"" + pi[j].Name.ToString(CultureInfo.InvariantCulture) + "\":\"\"");
                        }
                        if (j < pi.Length - 1)
                        {
                            json.Append(",");
                        }
                    }
                    json.Append("}");
                    if (i < list.Count - 1)
                    {
                        json.Append(",");
                    }
                }
            }
            json.Append("]}");
            return json.ToString();
        }

        /// <summary>
        /// List转成json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static string ListToGridJson<T>(IList<T> list, int total)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{\"total\":" + total + ",");
            json.Append("\"rows\":[");
            if (list != null && list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    PropertyInfo[] pi = obj.GetType().GetProperties();
                    json.Append("{");
                    for (int j = 0; j < pi.Length; j++)
                    {
                        object propertyValue = pi[j].GetValue(list[i], null);
                        if (propertyValue != null)
                        {
                            Type type = propertyValue.GetType();
                            string name = pi[j].Name;
                            json.Append("\"" + name.ToString(CultureInfo.InvariantCulture) + "\":" +
                                        StringFormat(pi[j].GetValue(list[i], null).ToString(), type));
                        }
                        else
                        {
                            if (pi.Length > j)
                                json.Append("\"" + pi[j].Name.ToString(CultureInfo.InvariantCulture) + "\":\"\"");
                        }
                        if (j < pi.Length - 1)
                        {
                            json.Append(",");
                        }
                    }
                    json.Append("}");
                    if (i < list.Count - 1)
                    {
                        json.Append(",");
                    }
                }
            }
            json.Append("]}");
            return json.ToString();
        }

        /// <summary>
        /// List转成json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public static string ListToGridJson<T>(IList<T> list, int page, int rows)
        {
            page = (page == 0) ? 1 : page;
            rows = (rows == 0) ? 10 : rows;
            int start = (page - 1) * rows;
            int end = page * rows;
            end = (end > list.Count) ? list.Count : end;
            StringBuilder json = new StringBuilder();
            json.Append("{\"total\":" + list.Count + ",");
            json.Append("\"rows\":[");
            if (list.Count > 0)
            {
                for (int i = start; i < end; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    PropertyInfo[] pi = obj.GetType().GetProperties();
                    json.Append("{");
                    for (int j = 0; j < pi.Length; j++)
                    {
                        object propertyValue = pi[j].GetValue(list[i], null);
                        if (propertyValue != null)
                        {
                            Type type = propertyValue.GetType();
                            json.Append("\"" + pi[j].Name.ToString(CultureInfo.InvariantCulture) + "\":" +
                                        StringFormat(pi[j].GetValue(list[i], null).ToString(), type));
                        }
                        else
                        {
                            json.Append("\"" + pi[j].Name.ToString(CultureInfo.InvariantCulture) + "\":\"\"");
                        }
                        if (j < pi.Length - 1)
                        {
                            json.Append(",");
                        }
                    }
                    json.Append("}");
                    if (i < list.Count - 1)
                    {
                        json.Append(",");
                    }
                }
            }
            json.Append("]}");
            return json.ToString();
        }

        /// <summary>   
        /// List转成json    
        /// </summary>   
        /// <typeparam name="T"></typeparam>   
        /// <param name="list"></param>   
        /// <returns></returns>   
        public static string ListToJson<T>(IList<T> list)
        {
            if (list == null || list.Count <= 0) return string.Empty;
            object obj = list[0];
            return ListToJson(list, obj.GetType().Name);
        }

        /// <summary>    
        /// 对象转换为Json字符串    
        /// </summary>    
        /// <param name="jsonObject">对象</param>    
        /// <returns>Json字符串</returns>    
        public static string ToJson(object jsonObject)
        {
            string jsonString = "{";
            PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
            for (int i = 0; i < propertyInfo.Length; i++)
            {
                object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
                string value;
                if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
                {
                    value = "'" + objectValue + "'";
                }
                else if (objectValue is string)
                {
                    value = "'" + ToJson(objectValue.ToString()) + "'";
                }
                else if (objectValue is IEnumerable)
                {
                    value = ToJson(objectValue);
                }
                else
                {
                    value = ToJson(objectValue.ToString());
                }
                jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ",";
            }
            return jsonString.Remove(jsonString.Length - 1, jsonString.Length) + "}";
        }

        ///// <summary>    
        ///// 对象集合转换Json    
        ///// </summary>    
        ///// <param name="array">集合对象</param>    
        ///// <returns>Json字符串</returns>    
        //public static string ToJson(IEnumerable array)
        //{
        //    string jsonString = "[";
        //    foreach (object item in array)
        //    {
        //        jsonString += ToJson(item) + ",";
        //    }
        //    jsonString.Remove(jsonString.Length - 1, jsonString.Length);
        //    return jsonString + "]";
        //}

        /// <summary>    
        /// 普通集合转换Json    
        /// </summary>    
        /// <param name="array">集合对象</param>    
        /// <returns>Json字符串</returns>    
        public static string ToArrayString(IEnumerable array)
        {
            string jsonString = "[";
            foreach (object item in array)
            {
                jsonString = ToJson(item.ToString()) + ",";
            }
            return jsonString.Remove(jsonString.Length - 1, jsonString.Length) + "]";
        }

        /// <summary>
        /// Datatable转换为Json    
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>

        public static string ToJson(DataTable dt)
        {
            StringBuilder jsonString = new StringBuilder();
            jsonString.Append("[");
            DataRowCollection drc = dt.Rows;
            for (int i = 0; i < drc.Count; i++)
            {
                jsonString.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string strKey = dt.Columns[j].ColumnName;
                    string strValue = drc[i][j].ToString();
                    Type type = dt.Columns[j].DataType;
                    jsonString.Append("\"" + strKey + "\":");
                    strValue = StringFormat(strValue, type);
                    if (j < dt.Columns.Count - 1)
                    {
                        jsonString.Append(strValue + ",");
                    }
                    else
                    {
                        jsonString.Append(strValue);
                    }
                }
                jsonString.Append("},");
            }
            jsonString.Remove(jsonString.Length - 1, 1);
            jsonString.Append("]");
            return jsonString.ToString();
        }

        /// <summary>   
        /// DataTable转成Json    
        /// </summary>   
        /// <param name="jsonName"></param>   
        /// <param name="dt"></param>   
        /// <returns></returns>   
        public static string ToJson(DataTable dt, string jsonName)
        {
            StringBuilder json = new StringBuilder();
            if (string.IsNullOrEmpty(jsonName))
                jsonName = dt.TableName;
            json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Type type = dt.Rows[i][j].GetType();
                        json.Append("\"" + dt.Columns[j].ColumnName.ToString(CultureInfo.InvariantCulture) + "\":" +
                                    StringFormat(dt.Rows[i][j].ToString(), type));
                        if (j < dt.Columns.Count - 1)
                            json.Append(",");
                    }
                    json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        json.Append(",");
                    }
                }
            }
            json.Append("]}");
            return json.ToString();
        }

        /// <summary>    
        /// DataReader转换为Json    
        /// </summary>    
        /// <param name="dataReader">DataReader对象</param>    
        /// <returns>Json字符串</returns>    
        public static string ToJson(DbDataReader dataReader)
        {
            StringBuilder jsonString = new StringBuilder();
            jsonString.Append("[");
            while (dataReader.Read())
            {
                jsonString.Append("{");
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    Type type = dataReader.GetFieldType(i);
                    string strKey = dataReader.GetName(i);
                    string strValue = dataReader[i].ToString();
                    jsonString.Append("\"" + strKey + "\":");
                    strValue = StringFormat(strValue, type);
                    if (i < dataReader.FieldCount - 1)
                    {
                        jsonString.Append(strValue + ",");
                    }
                    else
                    {
                        jsonString.Append(strValue);
                    }
                }
                jsonString.Append("},");
            }
            dataReader.Close();
            jsonString.Remove(jsonString.Length - 1, 1);
            jsonString.Append("]");
            return jsonString.ToString();
        }

        /// <summary>    
        /// DataSet转换为Json    
        /// </summary>    
        /// <param name="dataSet">DataSet对象</param>    
        /// <returns>Json字符串</returns>    
        public static string ToJson(DataSet dataSet)
        {
            string jsonString = "{";
            foreach (DataTable table in dataSet.Tables)
            {
                jsonString += "\"" + table.TableName + "\":" + ToJson(table) + ",";
            }
            jsonString = jsonString.TrimEnd(',');
            return jsonString + "}";
        }

        /// <summary>   
        /// 过滤特殊字符   
        /// </summary>   
        /// <param name="s"></param>   
        /// <returns></returns>   
        private static string String2Json(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '\\':
                        sb.Append("\\\\");
                        break;
                    case '/':
                        sb.Append("\\/");
                        break;
                    case '\b':
                        sb.Append("\\b");
                        break;
                    case '\f':
                        sb.Append("\\f");
                        break;
                    case '\n':
                        sb.Append("\\n");
                        break;
                    case '\r':
                        sb.Append("\\r");
                        break;
                    case '\t':
                        sb.Append("\\t");
                        break;
                    default:
                        sb.Append(c);
                        break;
                }
            }
            return sb.ToString();
        }

        /// <summary>   
        /// 格式化字符型、日期型、布尔型   
        /// </summary>   
        /// <param name="str"></param>   
        /// <param name="type"></param>   
        /// <returns></returns>   
        private static string StringFormat(string str, Type type)
        {
            if (type == typeof(string))
            {
                str = String2Json(str);
                str = "\"" + str + "\"";
            }
            else if (type == typeof(DateTime))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
            else if (type.BaseType == typeof(Enum))
            {
                str = "\"" + str + "\"";
            }
            return str;
        }

        /// <summary>
        /// JSON序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string JsonSerializer<T>(T model)
        {
            #region 微软自带的序列化成jison的工具

            //System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            //string stmp = serializer.Serialize(apiOrder.Reply.OrderPayInfo);

            #endregion

            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); //这里使用自定义日期格式，默认是ISO8601格式         
            timeConverter.DateTimeFormat = "yyyy-MM-dd"; //设置时间格式 
            return JavaScriptConvert.SerializeObject(model, timeConverter);
        }
        public static string JsonSerializerDateTime<T>(T model)
        {
            #region 微软自带的序列化成jison的工具

            //System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            //string stmp = serializer.Serialize(apiOrder.Reply.OrderPayInfo);

            #endregion

            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); //这里使用自定义日期格式，默认是ISO8601格式         
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; //设置时间格式 
            return JavaScriptConvert.SerializeObject(model, timeConverter);
        }

        /// <summary>
        /// JSON反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(string jsonString)
        {
            #region 微软自带

            //using System; 
            // using System.Collections.Generic; 
            //using System.Linq;  
            //using System.Web;  
            //using System.Runtime.Serialization.Json;  
            //using System.IO; 
            //using System.Text;
            //DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            //MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            //T obj = (T)ser.ReadObject(ms);          
            //return obj;

            #endregion

            T deserializedProduct = (T)JavaScriptConvert.DeserializeObject(jsonString, typeof(T));
            return deserializedProduct;
        }

        /// <summary>
        /// 提取POST内容为文本
        /// </summary>
        /// <returns>提取POST内容为文本</returns>
        public static string GetPostString()
        {
            try
            {
                //return new StreamReader(HttpContext.Current.Request.InputStream, Encoding.UTF8).ReadToEnd();
                string reader = new StreamReader(HttpContext.Current.Request.InputStream, Encoding.UTF8).ReadToEnd();
                if (reader.StartsWith("json="))
                {
                    reader = reader.Substring(5, reader.Length - 5).Split('&')[0];
                    reader = HttpUtility.UrlDecode(reader);
                }
                return reader;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 填充DataGrid分页的Json数据格式
        /// </summary>
        /// <param name="dt">分页数据集</param>
        /// <param name="total">总条数</param>
        /// <returns></returns>
        public static string OnDataGrid(DataTable dt, int total)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"total\":" + total + ",\"rows\":[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]}");
            return jsonBuilder.ToString();
        }

        /// <summary>
        /// 用于初始化表单的Json数据
        /// </summary>
        /// <param name="dt">数据集,仅一条记录</param>
        /// <returns></returns>
        public static string OnForm(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[0][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("}");
            }
            return jsonBuilder.ToString();
        }

        /// <summary>
        /// 获得模糊查询条件。
        /// </summary>
        /// <param name="dictionary">Model类表实体的集合</param>
        /// <returns>获得拼接的查询条件，如"and name='xxx' and adress='北京'"</returns>
        public static string GetQueryFilterString(IDictionary<string, string> dictionary)
        {
            return GetQueryFilterString(dictionary, true);
        }
        /// <summary>
        /// 查询条件 
        /// </summary>
        /// <param name="dictionary"></param>
        /// <param name="isLike">是否模糊查询</param>
        /// <returns></returns>
        public static string GetQueryFilterString(IDictionary<string, string> dictionary, bool isLike)
        {
            StringBuilder str = new StringBuilder();
            //page ,rows两个要给去掉
            foreach (KeyValuePair<string, string> item in dictionary)
            {
                if (!string.IsNullOrEmpty(item.Value.Trim()) && item.Key != "page" && item.Key != "rows" && item.Key.ToLower() != "createtime" && item.Key != "UserHostAddress")//创建时间不为查询条件
                {
                    DateTime outDate;
                    bool isDate = DateTime.TryParse(item.Value, out outDate);
                    if (isLike && !isDate)
                        str.Append(" AND " + item.Key + " like '%" + item.Value + "%' ");
                    else
                        str.Append(" AND " + item.Key + " = '" + item.Value + "'");
                }
            }

            return str.ToString();
        }

        //public string GetDepList(JsonReq req)
        //{
        //    try
        //    {
        //        DataTable dt = DbHelperSQL.Query("select * from dmt_bmdm");
        //        return getDepartJson(dt);
        //    }
        //    catch (Exception ex)
        //    {
        //        return JsonRet.New((int)command.infocn.未知错误, ex.Message, null).ToJson();
        //    }
        //}

        public static string GetTreeJson<T>(List<T> list, string id, string text, string children, string clsIcon, string check, string state)
        {
            if (list == null || list.Count <= 0) return string.Empty;
            StringBuilder builder = new StringBuilder();
            builder.Append("[");
            Type type = typeof(T);
            foreach (T t1 in list)
            {
                builder.Append("{\"id\":" + type.GetProperty(id).GetValue(t1, null) + ",\"text\":\"" + type.GetProperty(text).GetValue(t1, null) + "\"");
                if (!string.IsNullOrEmpty(clsIcon))
                    builder.Append(",\"iconCls\":\"" + type.GetProperty(clsIcon).GetValue(t1, null) + "\"");
                if (!string.IsNullOrEmpty(check))
                    builder.Append(",\"checked\": " + type.GetProperty(check).GetValue(t1, null).ToString().ToLower() + "");
                if (!string.IsNullOrEmpty(state))
                {
                    builder.Append(",\"state\":\"" + type.GetProperty(state).GetValue(t1, null) + "\"");
                }
                object obj = type.GetProperty(children).GetValue(t1, null);
                if (obj != null)
                {
                    List<T> list2 = obj as List<T>;
                    if (list2 != null && list2.Count > 0)
                    {
                        builder.Append(",\"children\":");
                        string str = GetTreeJson<T>(list2, id, text, children, clsIcon, check, state);
                        builder.Append(str);
                    }
                    // builder.Append("]");
                }
                obj = type.GetProperty("Attributes").GetValue(t1, null);
                if (obj != null)
                {
                    builder.Append(",\"attributes\":{");
                    Dictionary<string, string> attributes = obj as Dictionary<string, string>;
                    foreach (KeyValuePair<string, string> keyValuePair in attributes)
                    {
                        builder.Append("\"" + keyValuePair.Key + "\":\"" + keyValuePair.Value + "\",");
                    }
                    builder.Remove(builder.Length - 1, 1);
                    builder.Append("}");
                }
                builder.Append("},");
            }
            builder.Remove(builder.Length - 1, 1);
            builder.Append("]");
            return builder.ToString();
        }

        public static T JsonDeserialize<T>(Dictionary<string, string> dictionary) where T : new()
        {
            T t = new T();
            Type type = t.GetType();
            foreach (PropertyInfo info in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (dictionary.ContainsKey(info.Name) && !string.IsNullOrEmpty(dictionary[info.Name]))
                {
                    string typeName = info.PropertyType.FullName;                    
                    object value = dictionary[info.Name];
                    if (typeName.Contains("Char"))
                    {
                        info.SetValue(t, Convert.ToChar(value), null);
                    }
                    else if (typeName.Contains("Int32"))
                    {
                        // value = Convert.ToInt32(value);
                        info.SetValue(t, Convert.ToInt32(value), null);
                    }
                    else if (typeName.Contains("DateTime"))
                    {
                        // value = Convert.ToDateTime(value);
                        info.SetValue(t, Convert.ToDateTime(value), null);
                    }
                    else if (typeName.Contains("float") || typeName.Contains("Single"))
                    {
                        //value = float.Parse(dictionary[info.Name]);
                        info.SetValue(t, float.Parse(dictionary[info.Name]), null);
                    }
                    else if (typeName.Contains("Decimal"))
                    {
                        //  value = Convert.ToDecimal(value);
                        info.SetValue(t, Convert.ToDecimal(value), null);
                    }
                    else if (typeName.Contains("Boolean"))
                    {
                        //value = Convert.ToBoolean(value);
                        info.SetValue(t, Convert.ToBoolean(value), null);
                    }
                    else
                        info.SetValue(t, value, null);
                }
            }
            return t;
        }
        public static string ResultToJson(bool bl, ExecuteType type)
        {
            string msg = string.Empty;
            if (type == ExecuteType.Add) msg = bl ? "添加成功!" : "添加失败!";
            else if (type == ExecuteType.Update) msg = bl ? "修改成功!" : "修改失败!";
            else if (type == ExecuteType.Delete) msg = bl ? "删除成功!" : "删除失败!";
            return "{ \"flag\":\"" + bl.ToString(CultureInfo.InvariantCulture) + "\",\"msg\":\"" + msg + "\"}";
        }
        public static string ResultToJson(bool bl, string info)
        {
            return "{ \"flag\":\"" + bl.ToString(CultureInfo.InvariantCulture) + "\",\"msg\":\"" + info + "\"}";
        }
    }

    public enum ExecuteType
    {
        Add,
        Update,
        Delete
    }
}
